package com.tx.activemq._03_protocol;


import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.IOException;

/**
 * 传输协议： 消费者
 */
public class Consumer {

    private static final String DEFAULT_URL = "nio://192.168.254.30:61618";

    private static final String QUEUE_NAME = "queue_01";

    public static void main(String[] args) throws JMSException, IOException {
        System.out.println("******1号消费者");
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(DEFAULT_URL);
        Connection connection = factory.createConnection();
        connection.start();

        Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(QUEUE_NAME);

        MessageConsumer consumer = session.createConsumer(queue);
        consumer.setMessageListener((msg) -> {
            if(msg != null && msg instanceof  TextMessage){
                TextMessage textMessage = (TextMessage) msg;
                try {
                    System.out.println(textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });
        System.in.read();
        consumer.close();
        session.close();
        connection.close();
    }
}